package br.com.tecvel.control;

import br.com.tecvel.control.conexao.Conexao;
import br.com.tecvel.model.Cliente;
import java.util.List;
import javax.persistence.Query;

/**
 *
 * @author RAFAEL
 */
public class ClienteControl {
    private Conexao con;
    public ClienteControl() {
        con = new Conexao();
    }
    
    public boolean criar(Cliente c){
        boolean r = true;
        try{
            con.conectar();
            con.getEntityTransaction().begin();
            con.getEntityManager().persist(c);
            con.getEntityTransaction().commit();
        }catch(Exception e){
            r = false;
            System.err.println("ERRO AO CADASTRAR CLIETE : "+e.getStackTrace());
        }finally{
            con.getEntityManager().close();
        }
        return r;
    }
    public boolean editar(Cliente c){
        boolean r = true;
        try{
            con.conectar();
            con.getEntityTransaction().begin();
            con.getEntityManager().merge(c);
            con.getEntityTransaction().commit();
        }catch(Exception e){
            r = false;
            System.err.println("ERRO AO EDITAR CLIETE : "+e.getStackTrace());
        }finally{
            con.getEntityManager().close();
        }
        return r;
    }
    public boolean exluir(Cliente c){
        boolean r = true;
        try{
            con.conectar();
            con.getEntityTransaction().begin();
            Cliente cliente = con.getEntityManager().merge(c);
            con.getEntityManager().remove(cliente);
            con.getEntityTransaction().commit();
        }catch(Exception e){
            r = false;
            System.err.println("ERRO AO EXCLUIR CLIETE : "+e.getStackTrace());
        }finally{
        
        }
        return r;
    }
    public List<Cliente> listarPorNome(String nome){
        List<Cliente> listaCliente = null;
		try{
                    con.conectar();
                    con.getEntityTransaction().begin();
                    Query query = con.getEntityManager().createNativeQuery("select * from Cliente where nome like ? ", Cliente.class).setParameter("1", "%"+nome+"%");
                    con.getEntityTransaction().commit();
                    listaCliente =  query.getResultList();

		}catch(Exception e){
                    System.out.println("Erro ao pesqusiar "+e.getStackTrace());
		}finally{
                    con.getEntityManager().close();
		}
                return listaCliente;
    }
    public List<Cliente> findCpf(String cpf) {
		List<Cliente> listaCliente = null;
		try{
                    con.conectar();
                    con.getEntityTransaction().begin();
                    //Query query = con.getEntityManager().createNamedQuery("Cliente.findNome").setParameter("nome", nome);
                    Query query = con.getEntityManager().createNativeQuery("select * from Cliente where cpf like ? ", Cliente.class).setParameter("1", "%"+cpf+"%");
                    con.getEntityTransaction().commit();
                    listaCliente =  query.getResultList();

		}catch(Exception e){
                    System.out.println("Erro ao pesqusiar "+e.getStackTrace());
		}finally{
                    con.getEntityManager().close();
		}
		return listaCliente;
	}
    public List<Cliente> findAll() {
		List<Cliente> listaCliente = null;
		try{
			con.conectar();
			con.getEntityTransaction().begin();
			Query query = con.getEntityManager().createNamedQuery("Cliente.findAll");
			con.getEntityTransaction().commit();
			listaCliente =   query.getResultList();
		}catch(Exception e){
			System.out.println("Erro ao pesqusiar "+e.getStackTrace());
		}finally{
			con.getEntityManager().close();
		}
		return listaCliente;
	}
}
